package com.serookie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serookie.entity.Goods;
import com.serookie.entity.GoodsCategory;
import com.serookie.entity.GoodsDetails;
import com.serookie.mapper.GoodsCategoryMapper;
import com.serookie.mapper.GoodsDetailsMapper;
import com.serookie.mapper.GoodsMapper;
import com.serookie.service.GoodsCategoryService;
import com.serookie.service.GoodsDetailsService;
import com.serookie.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author kevintam
 * @version 1.0
 * @date 2021/5/13 0:18
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService{

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsDetailsMapper goodsDetailsMapper;

    @Autowired
    private GoodsDetailsService detailsService;

    @Override
    public Boolean addGoods(Goods goods) {
        return goodsMapper.insert(goods)>0;
    }

    @Override
    public List<Goods> all() {
        List<Goods> goods = goodsMapper.selectList(null);
        return goods;
    }

    @Override
    public Goods GoodsById(Integer goodsId) {
        Goods goods = goodsMapper.selectById(goodsId);
        List<GoodsDetails> goodsDetailsList = detailsService.getByGoodsId(goods.getGoodsId());
        goods.setDetails(goodsDetailsList);
        return goods;
    }

    @Override
    public List<Goods> GoodsByName(String goodsName) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper();
        queryWrapper.like("goods_name",goodsName);
        List<Goods> goods = goodsMapper.selectList(queryWrapper);
        goods.forEach(element->{
            List<GoodsDetails> goodsDetails = detailsService.getByGoodsId(element.getGoodsId());
            element.setDetails(goodsDetails);
        });
        return goods;
    }

    @Override
    public Boolean deleteGoods(Integer goodsId) {
        return goodsMapper.deleteById(goodsId)>0;
    }

    @Override
    public Boolean updateGoods(Goods goods) {
        return goodsMapper.updateById(goods)>0;
    }

    @Override
    public List<Goods> getByCatId(Integer catId) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper();
        queryWrapper.eq("category_three_id",catId);
        List<Goods> goods = goodsMapper.selectList(queryWrapper);
        goods.forEach(element->{
            List<GoodsDetails> details = detailsService.getByGoodsId(element.getGoodsId());
            element.setDetails(details);
        });
        return goods;
    }

    @Override
    public Integer goodsCount() {
        return goodsMapper.selectCount(null);
    }
}
